﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceProcess;
using DemoLib.Service.Services;
using DemoLib.Utilities.ServiceUtilities;

namespace DemoService.SelfHost
{
    //TO create installer, go to design view of this service and right click.then click on Add installer
    partial class DemoService : ServiceBase
    {
        public DemoService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                StartServices();
            }
            catch (Exception exception)
            {
                DemoLib.Utilities.LoggingUtilities.Log.LogError("Error while starting service. " + exception.Message);
                throw;
            }
        }


        protected override void OnStop()
        {
            try
            {
                StopServices();
            }
            catch (Exception exception)
            {
                DemoLib.Utilities.LoggingUtilities.Log.LogError("Error while stopping service. " + exception.Message );
            }
        }

        #region Private methods

        private ServiceHost _authServiceHost;
        private ServiceHost _userServiceHost;
        private ServiceHost _emailServiceHost;
        private ServiceHost _infoServiceHost;

        #region note for port sharing service
            //type services.msc and set "NetTcpPortSharing" start type to manual
            // start service 
            //net start NetTcpPortSharing
            // add SID in to SMSvcHost.exe.config (C:\Windows\Microsoft.NET\Framework\v4.0.30319) or task mgr (services)-> NetTcpPortSharing -> go to process -> go to file location 
            // get SID wmic useraccount get name,sid
            #endregion

        private void StartServices()
        {
            _authServiceHost = new GenericServiceHost<AuthoService>();
            _authServiceHost.Open();

            _userServiceHost = new GenericServiceHost<UserService>();
            _userServiceHost.Open();

            _emailServiceHost = new GenericServiceHost<EmailService>();
            _emailServiceHost.Open();

            _infoServiceHost = new GenericServiceHost<InfoService>();
            _infoServiceHost.Open();

        }

        private void StopServices()
        {
            if (_authServiceHost != null)
                _authServiceHost.Close();

            if (_userServiceHost != null) 
                _userServiceHost.Close();

            if (_emailServiceHost != null) 
                _emailServiceHost.Close();

            if (_infoServiceHost != null)
                _infoServiceHost.Close();
        }

        #endregion

        public static void Main(string[] args)
        {
            using (var demoService = new DemoService())
            {                
                demoService.OnStart(args);
                Console.WriteLine("Press any key to exit...");
                Console.ReadLine();
                Console.WriteLine("Closing...");
                demoService.OnStop();
            }
        }
    }
}
